package com.bawei.mdm2207alast.view

import android.annotation.SuppressLint
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.offset
import androidx.compose.material3.Badge
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.bawei.mdm2207alast.R
import com.bawei.mdm2207alast.home.HomePage
import com.bawei.mdm2207alast.message.MessagePage
import com.bawei.mdm2207alast.mine.MinePage

/**
 * 作者：赵亮
 * 时间：2024/12/16 14:14
 * 功能：底部导航页面
 */

/**
 * 底部导航数据类
 * @param img 底部导航图标
 * @param text 底部导航文字
 */
data class BottomEntity(val img: Int, val text: String)

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun MainPage(navController: NavController = rememberNavController()) {

    // 初始化底部导航数据
    val list = listOf(
        BottomEntity(R.drawable.icon1275, "消息"),
        BottomEntity(R.drawable.icon64, "门户"),
        BottomEntity(R.drawable.icon1405, "我的"),
    )

    // 底部导航选中， 默认选中门户
    var selectedIndex by remember {
        mutableStateOf(1)
    }

    Scaffold(
        // 底部
        bottomBar = {
            NavigationBar {
                list.forEachIndexed { index, bottomEntity ->
                    NavigationBarItem(
                        selected = selectedIndex == index,
                        onClick = { selectedIndex = index },
                        label = { Text(text = bottomEntity.text) },
                        icon = {
                            Box {
                                Image(
                                    painter = painterResource(id = bottomEntity.img),
                                    contentDescription = null
                                )
                                // 消息提示
                                if (index == 0) {
                                    Badge(
                                        modifier = Modifier
                                            .align(Alignment.TopEnd)
                                            .offset(x = 10.dp, y = -10.dp)
                                    ) {
                                        Text(text = "10")
                                    }
                                }
                            }
                        })
                }
            }
        }
    ) {
        when (selectedIndex) {
            0 -> MessagePage(navController)
            1 -> HomePage(navController)
            2 -> MinePage(navController)
        }
    }
}

@Preview(showSystemUi = true)
@Composable
fun MainPagePreview() {
    MainPage()
}

